#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv/highgui.h>
#include <time.h>

using namespace cv;

int main(int argc, char **argv)
{
  /* initialize camera */
  VideoCapture capture = VideoCapture(0);

  Mat frame;
  
  if(!capture.isOpened()) {
    fprintf( stderr, "Cannot open initialize webcam!\n");
    return 1;
  }

  cvNamedWindow("Original", CV_WINDOW_AUTOSIZE);
  cvNamedWindow("JPEG", CV_WINDOW_AUTOSIZE);
  cvNamedWindow("Difference", CV_WINDOW_AUTOSIZE);
  cvNamedWindow("Reconstructed", CV_WINDOW_AUTOSIZE);
  
  for(int c=0;c<10;c++) {
    if(!capture.read(frame)) {
      c--;
      continue;
    }
  }
  vector<int> param;
  param.push_back(CV_IMWRITE_PNG_COMPRESSION);
  param.push_back(9);
  std::cout << "BEGIN\n";
  clock_t start = clock();
  for(int c=0;c<100;c++) {
    vector<uchar> buf;
    if(!imencode("*.png",frame,buf,param)) {
      break;
    }
    //jpeg = imdecode(buf,1);
  }
  std::cout << (clock() - start) / (double)CLOCKS_PER_SEC << "\n";
  vector<uchar> buf;
  if(!imencode("*.png",frame,buf,param)) {
    
  }
  std::cout << (int)buf.size() << "\n";
  

  return 0;
}
